package com.diandian.backend.product.service;

import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.response.PageResult;
import com.diandian.backend.product.domain.ProductOfflineInfoSku;
import com.diandian.backend.product.dto.*;
import com.diandian.backend.product.mapper.ProductOfflineInfoSkuMapper;
import com.diandian.backend.product.resp.ProductOfflineInfoSkuDetailResp;
import com.diandian.backend.product.resp.ProductOfflineInfoSkuPageResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.diandian.backend.product.resp.ProductOfflineInfoSkuListResp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ProductOfflineInfoSkuService {

    @Autowired
    private ProductOfflineInfoPictureVideoService productOfflineInfoPictureVideoService;

    @Autowired
    private ProductOfflineInfoSkuMapper productOfflineInfoSkuMapper;

    public PageResult page(ProductOfflineInfoSkuPageParamDTO requestDTO){

        requestDTO.check();
        int pageNum = requestDTO.getPageNum();
        int pageSize = requestDTO.getPageSize();

        // 查询总数
        int totalCount = productOfflineInfoSkuMapper.getCount(requestDTO);

        // 查询数据集合
        List<ProductOfflineInfoSku> dataList = productOfflineInfoSkuMapper.getPage(requestDTO, requestDTO.getPageSize(), (requestDTO.getPageNum() - 1) * requestDTO.getPageSize());

        // 组装数据
        List<ProductOfflineInfoSkuPageResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                ProductOfflineInfoSkuPageResp item = new ProductOfflineInfoSkuPageResp();
                item.setId(x.getId());
                item.setProductCode(x.getProductCode());
                item.setCode(x.getCode());
                item.setName(x.getName());
                item.setDescription(x.getDescription());
                item.setCategoryCode(x.getCategoryCode());
                item.setBrandCode(x.getBrandCode());
                item.setRecommend(x.getRecommend());
                item.setOriginType(x.getOriginType());
                item.setOriginName(x.getOriginName());
                item.setPacking(x.getPacking());
                item.setSaleType(x.getSaleType());
                item.setSellPoint(x.getSellPoint());
                item.setReturnWithoutReason(x.getReturnWithoutReason());
                item.setReturnWithoutReasonDays(x.getReturnWithoutReasonDays());
                item.setSaveWay(x.getSaveWay());
                item.setModel(x.getModel());
                item.setPrice(x.getPrice());
                item.setUpc(x.getUpc());
                item.setUpcImage(x.getUpcImage());
                item.setColor(x.getColor());
                item.setShelvesCode(x.getShelvesCode());
                item.setWeight(x.getWeight());
                item.setWeightUnit(x.getWeightUnit());
                item.setLength(x.getLength());
                item.setWidth(x.getWidth());
                item.setHeight(x.getHeight());
                item.setQualityGuaranteePeriod(x.getQualityGuaranteePeriod());
                item.setQualityGuaranteePeriodUnit(x.getQualityGuaranteePeriodUnit());
                item.setOnShelvesStatus(x.getOnShelvesStatus());
                item.setUseScope(x.getUseScope());
                item.setExtend(x.getExtend());
                item.setCreateUserId(x.getCreateUserId());
                item.setCreateUesrName(x.getCreateUesrName());
                item.setCreateTime(x.getCreateTime());
                item.setUpdateUserId(x.getUpdateUserId());
                item.setUpdateUserName(x.getUpdateUserName());
                item.setUpdateTime(x.getUpdateTime());
                item.setDel(x.getDel());
             respList.add(item);
            });
        }

        return new PageResult(pageNum, pageSize, totalCount, respList);
    }

    public List<ProductOfflineInfoSkuListResp> getList(ProductOfflineInfoSkuListParamDTO requestDTO){

        requestDTO.check();

        // 查询数据集合
        List<ProductOfflineInfoSku> dataList = productOfflineInfoSkuMapper.getList(requestDTO);

        // 组装数据
        List<ProductOfflineInfoSkuListResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                ProductOfflineInfoSkuListResp item = new ProductOfflineInfoSkuListResp();
                item.setId(x.getId());
                item.setBelong(x.getBelong());
                item.setProductCode(x.getProductCode());
                item.setCode(x.getCode());
                item.setName(x.getName());
                item.setDescription(x.getDescription());
                item.setCategoryCode(x.getCategoryCode());
                item.setBrandCode(x.getBrandCode());
                item.setRecommend(x.getRecommend());
                item.setOriginType(x.getOriginType());
                item.setOriginName(x.getOriginName());
                item.setPacking(x.getPacking());
                item.setSaleType(x.getSaleType());
                item.setSellPoint(x.getSellPoint());
                item.setReturnWithoutReason(x.getReturnWithoutReason());
                item.setReturnWithoutReasonDays(x.getReturnWithoutReasonDays());
                item.setSaveWay(x.getSaveWay());
                item.setModel(x.getModel());
                item.setPrice(x.getPrice());
                item.setUpc(x.getUpc());
                item.setUpcImage(x.getUpcImage());
                item.setColor(x.getColor());
                item.setShelvesCode(x.getShelvesCode());
                item.setWeight(x.getWeight());
                item.setWeightUnit(x.getWeightUnit());
                item.setLength(x.getLength());
                item.setWidth(x.getWidth());
                item.setHeight(x.getHeight());
                item.setQualityGuaranteePeriod(x.getQualityGuaranteePeriod());
                item.setQualityGuaranteePeriodUnit(x.getQualityGuaranteePeriodUnit());
                item.setOnShelvesStatus(x.getOnShelvesStatus());
                item.setUseScope(x.getUseScope());
                item.setExtend(x.getExtend());
                item.setCreateUserId(x.getCreateUserId());
                item.setCreateUesrName(x.getCreateUesrName());
                item.setCreateTime(x.getCreateTime());
                item.setUpdateUserId(x.getUpdateUserId());
                item.setUpdateUserName(x.getUpdateUserName());
                item.setUpdateTime(x.getUpdateTime());
                item.setDel(x.getDel());
                respList.add(item);
            });
        }

        return respList;
    }

    public ProductOfflineInfoSkuDetailResp getDetail(ProductOfflineInfoSkuDetailParamDTO param) {

        ProductOfflineInfoSku dbData = productOfflineInfoSkuMapper.getDetail(param);
        if(dbData == null){
            return null;
        }

        ProductOfflineInfoSkuDetailResp result = new ProductOfflineInfoSkuDetailResp();
        result.setId(dbData.getId());
        result.setProductCode(dbData.getProductCode());
        result.setCode(dbData.getCode());
        result.setName(dbData.getName());
        result.setDescription(dbData.getDescription());
        result.setCategoryCode(dbData.getCategoryCode());
        result.setBrandCode(dbData.getBrandCode());
        result.setRecommend(dbData.getRecommend());
        result.setOriginType(dbData.getOriginType());
        result.setOriginName(dbData.getOriginName());
        result.setPacking(dbData.getPacking());
        result.setSaleType(dbData.getSaleType());
        result.setSellPoint(dbData.getSellPoint());
        result.setReturnWithoutReason(dbData.getReturnWithoutReason());
        result.setReturnWithoutReasonDays(dbData.getReturnWithoutReasonDays());
        result.setSaveWay(dbData.getSaveWay());
        result.setModel(dbData.getModel());
        result.setPrice(dbData.getPrice());
        result.setUpc(dbData.getUpc());
        result.setUpcImage(dbData.getUpcImage());
        result.setColor(dbData.getColor());
        result.setShelvesCode(dbData.getShelvesCode());
        result.setWeight(dbData.getWeight());
        result.setWeightUnit(dbData.getWeightUnit());
        result.setLength(dbData.getLength());
        result.setWidth(dbData.getWidth());
        result.setHeight(dbData.getHeight());
        result.setQualityGuaranteePeriod(dbData.getQualityGuaranteePeriod());
        result.setQualityGuaranteePeriodUnit(dbData.getQualityGuaranteePeriodUnit());
        result.setOnShelvesStatus(dbData.getOnShelvesStatus());
        result.setUseScope(dbData.getUseScope());
        result.setExtend(dbData.getExtend());
        result.setCreateUserId(dbData.getCreateUserId());
        result.setCreateUesrName(dbData.getCreateUesrName());
        result.setCreateTime(dbData.getCreateTime());
        result.setUpdateUserId(dbData.getUpdateUserId());
        result.setUpdateUserName(dbData.getUpdateUserName());
        result.setUpdateTime(dbData.getUpdateTime());
        result.setDel(dbData.getDel());
        return result;
    }

    public void updateById(ProductOfflineInfoSkuUpdateByIdParamDTO param) {
        param.check();

        ProductOfflineInfoSkuDetailParamDTO queryDetailDTO = new ProductOfflineInfoSkuDetailParamDTO();
        queryDetailDTO.setId(param.getId());
        ProductOfflineInfoSku dbData = productOfflineInfoSkuMapper.getDetail(queryDetailDTO);
        if(dbData == null){
            throw new DefaultException("更新的记录不存在");
        }

        productOfflineInfoSkuMapper.updateById(param);
    }

    public void deleteById(ProductOfflineInfoSkuDeleteByIdParamDTO paramDTO) {
        paramDTO.check();
        productOfflineInfoSkuMapper.deleteById(paramDTO);
    }

    public void deleteByProductCode(String belong, String productCode) {

        if(productCode == null || "".equals(productCode)){
            throw new DefaultException("商品编码不能为空");
        }
        if(belong == null || "".equals(belong)){
            throw new DefaultException("归属标识不能为空");
        }

        // 删除sku信息
        productOfflineInfoSkuMapper.deleteByProductCode(belong, productCode);

        // todo 删除sku图片和视频信息

    }

    public void add(ProductOfflineInfoSkuAddParamDTO param) {
        productOfflineInfoSkuMapper.add(param);
    }

    public void batchAdd(List<ProductOfflineInfoSkuAddParamDTO> dataList) {
        if(dataList == null || dataList.size() == 0){
            return;
        }
        dataList.forEach(x -> {
            x.setCreateTime(new Date());
            x.setDel(0);
        });

        // 保存基础数据
        productOfflineInfoSkuMapper.batchAdd(dataList);

        // 保存文件
        List<ProductOfflineInfoPictureVideoAddParamDTO> fileList = new ArrayList<>();
        for (int i = 0; i < dataList.size(); i++) {
            if(dataList.get(i).getFileList() != null && dataList.get(i).getFileList().size() > 0){
                String belong = dataList.get(i).getBelong();
                dataList.get(i).getFileList().forEach(x -> {
                    x.setBelong(belong);
                });
                fileList.addAll(dataList.get(i).getFileList());
            }
        }
        if(fileList != null && fileList.size() > 0){
            productOfflineInfoPictureVideoService.batchAdd(fileList);
        }
    }
}
